<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>timezonedetection.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_locale_timezonedetection.js.html">timezonedetection.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Functions for detecting user&#39;s time zone.
<a name="line17"></a> * This work is based on Charlie Luo and Hong Yan&#39;s time zone detection work
<a name="line18"></a> * for CBG.
<a name="line19"></a> */
<a name="line20"></a>goog.provide(&#39;goog.locale.timeZoneDetection&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.locale&#39;);
<a name="line23"></a>goog.require(&#39;goog.locale.TimeZoneFingerprint&#39;);
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Array of time instances for checking the time zone offset.
<a name="line28"></a> * @type {Array.&lt;number&gt;}
<a name="line29"></a> * @private
<a name="line30"></a> */
<a name="line31"></a>goog.locale.timeZoneDetection.TZ_POKE_POINTS_ = [
<a name="line32"></a>  1109635200, 1128902400, 1130657000, 1143333000, 1143806400, 1145000000,
<a name="line33"></a>  1146380000, 1152489600, 1159800000, 1159500000, 1162095000, 1162075000,
<a name="line34"></a>  1162105500];
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * Calculates time zone fingerprint by poking time zone offsets for 13
<a name="line39"></a> * preselected time points.
<a name="line40"></a> * See {@link goog.locale.timeZoneDetection.TZ_POKE_POINTS_}
<a name="line41"></a> * @param {Date} date Date for calculating the fingerprint.
<a name="line42"></a> * @return {number} Fingerprint of user&#39;s time zone setting.
<a name="line43"></a> */
<a name="line44"></a>goog.locale.timeZoneDetection.getFingerprint = function(date) {
<a name="line45"></a>  var hash = 0;
<a name="line46"></a>  var stdOffset;
<a name="line47"></a>  var isComplex = false;
<a name="line48"></a>  for (var i = 0;
<a name="line49"></a>       i &lt; goog.locale.timeZoneDetection.TZ_POKE_POINTS_.length; i++) {
<a name="line50"></a>    date.setTime(goog.locale.timeZoneDetection.TZ_POKE_POINTS_[i] * 1000);
<a name="line51"></a>    var offset = date.getTimezoneOffset() / 30 + 48;
<a name="line52"></a>    if (i == 0) {
<a name="line53"></a>      stdOffset = offset;
<a name="line54"></a>    } else if (stdOffset != offset) {
<a name="line55"></a>      isComplex = true;
<a name="line56"></a>    }
<a name="line57"></a>    hash = (hash &lt;&lt; 2) ^ offset;
<a name="line58"></a>  }
<a name="line59"></a>  return isComplex ? hash : /** @type {number} */ (stdOffset);
<a name="line60"></a>};
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>/**
<a name="line64"></a> * Detects browser&#39;s time zone setting. If user&#39;s country is known, a better
<a name="line65"></a> * time zone choice could be guessed.
<a name="line66"></a> * @param {string=} opt_country Two-letter ISO 3166 country code.
<a name="line67"></a> * @param {Date=} opt_date Date for calculating the fingerprint. Defaults to the
<a name="line68"></a> *     current date.
<a name="line69"></a> * @return {string} Time zone ID of best guess.
<a name="line70"></a> */
<a name="line71"></a>goog.locale.timeZoneDetection.detectTimeZone = function(opt_country, opt_date) {
<a name="line72"></a>  var date = opt_date || new Date();
<a name="line73"></a>  var fingerprint = goog.locale.timeZoneDetection.getFingerprint(date);
<a name="line74"></a>  var timeZoneList = goog.locale.TimeZoneFingerprint[fingerprint];
<a name="line75"></a>  // Timezones in goog.locale.TimeZoneDetection.TimeZoneMap are in the format
<a name="line76"></a>  // US-America/Los_Angeles. Country code needs to be stripped before a
<a name="line77"></a>  // timezone is returned.
<a name="line78"></a>  if (timeZoneList) {
<a name="line79"></a>    if (opt_country) {
<a name="line80"></a>      for (var i = 0; i &lt; timeZoneList.length; ++i) {
<a name="line81"></a>        if (timeZoneList[i].indexOf(opt_country) == 0) {
<a name="line82"></a>          return timeZoneList[i].substring(3);
<a name="line83"></a>        }
<a name="line84"></a>      }
<a name="line85"></a>    }
<a name="line86"></a>    return timeZoneList[0].substring(3);
<a name="line87"></a>  }
<a name="line88"></a>  return &#39;&#39;;
<a name="line89"></a>};
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Returns an array of time zones that are consistent with user&#39;s platform
<a name="line94"></a> * setting. If user&#39;s country is given, only the time zone for that country is
<a name="line95"></a> * returned.
<a name="line96"></a> * @param {string=} opt_country 2 letter ISO 3166 country code. Helps in making
<a name="line97"></a> *     a better guess for user&#39;s time zone.
<a name="line98"></a> * @param {Date=} opt_date Date for retrieving timezone list. Defaults to the
<a name="line99"></a> *     current date.
<a name="line100"></a> * @return {Array.&lt;string&gt;} Array of time zone IDs.
<a name="line101"></a> */
<a name="line102"></a>goog.locale.timeZoneDetection.getTimeZoneList = function(opt_country,
<a name="line103"></a>    opt_date) {
<a name="line104"></a>  var date = opt_date || new Date();
<a name="line105"></a>  var fingerprint = goog.locale.timeZoneDetection.getFingerprint(date);
<a name="line106"></a>  var timeZoneList = goog.locale.TimeZoneFingerprint[fingerprint];
<a name="line107"></a>  if (!timeZoneList) {
<a name="line108"></a>    return [];
<a name="line109"></a>  }
<a name="line110"></a>  var chosenList = [];
<a name="line111"></a>  for (var i = 0; i &lt; timeZoneList.length; i++) {
<a name="line112"></a>    if (!opt_country || timeZoneList[i].indexOf(opt_country) == 0) {
<a name="line113"></a>      chosenList.push(timeZoneList[i].substring(3));
<a name="line114"></a>    }
<a name="line115"></a>  }
<a name="line116"></a>  return chosenList;
<a name="line117"></a>};
</pre>


</body>
</html>
